﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Tour.BLL;
using Tour.DAL;
using System.Text;
public partial class Hotel_MasterHotel :  VST.Controls.BaseLCGMasterPage
{
    public int? CustomerId { get; set; }
    public int? ReservationId { get; set; }
    public int? ReservationsDetailId { get; set; }   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["CustomerId"] != null)
        {
            CustomerId = Request["CustomerId"].ToInt();
        }
        else 
            CustomerId = null;//lấy mã khách hàng
        if (Request["ReservationId"] != null)
        {
            ReservationId = Request["ReservationId"].ToInt();
        }
        else ReservationId = null;
        if (Request["ReservationsDetailId"] != null)
        {
            ReservationsDetailId = Request["ReservationsDetailId"].ToInt();
        }
        else ReservationsDetailId = null;
        if (!IsPostBack)
        {
            //txtDate.Text = DateTime.Now.ToString("dd/mm/yyyy");
            LoadDataParentDDL();
        }
        
    }
    void LoadDataParentDDL()
    {
        List<TblRoomTypeEntity> ls = TblRoomTypeManager.CreateInstant().SelectTblRoomTypeBy(null, null, WorkingLanguage, "", null, null, "", "");
        ddlRoomType.DataSource = ls;
        ddlRoomType.DataTextField = "Name";
        ddlRoomType.DataValueField = "Id";
        ddlRoomType.DataBind();
        ddlRoomType.Items.Insert(0, new ListItem("------Loại Room----", "-1"));
    }
    TblCustomerEntity GetTblCustomer()
    {        
        TblCustomerEntity item = new TblCustomerEntity();       
            item.Name = txtName.Text;
            item.Email = txtEmail.Text;
            item.Address = txtAddress.Text;
            item.Phone = txtAddress.Text;
            item.BodyCard = "";
            item.BirthDay = DateTime.Now;
            item.BirthPlace = "";
            item.Visa = "";
            item.Guarantee = "";
            item.Entrydate = DateTime.Now;
            item.ResidenceDateFrom = DateTime.Now;
            item.ResidenceDateTo = DateTime.Now;
            item.Gender = true;
            item.Note = "";
            item.CreatedBy = HttpContext.Current.User.Identity.Name;
            item.CreatedDate = DateTime.Now;          
        return item;
    }
    TblReservationEntity GetTblReservation(int CustomerId)
    {        
        TblReservationEntity item = new TblReservationEntity();        
            item.TextId = "";
            item.CustomerId = CustomerId;
            item.CreatedBy = HttpContext.Current.User.Identity.Name;
            item.CreatedDate = DateTime.Now;          
        item.CustomerId = CustomerId;
        return item;
    }
    private List<TblReservationDetailEntity> GetTblReservationsDetail(int ReservationId)
    {
        List<TblReservationDetailEntity> ls = new List<TblReservationDetailEntity>();
        for (int i = 0; i < 1; i++)// vi chi co 1 detail thui
        {
            TblReservationDetailEntity obj =new  TblReservationDetailEntity();                          
                obj.TextId = "";
                obj.TypeRoomId = ddlRoomType.SelectedValue.ToInt();
                obj.NumberAdults = 2;
                obj.NumberChild = 2;
                obj.FromDate = DateTime.Now;
                obj.ToDate = DateTime.Now;
                obj.Promotion = 0;
                obj.Prepaid = 0;
                obj.CreatedBy = HttpContext.Current.User.Identity.Name;
                obj.CreatedDate = DateTime.Now;
                obj.ReservationId = ReservationId;          
            ls.Add(obj);
        }
        return ls;
    }
    protected void btnDatPhong_Click(object sender, EventArgs e)
    {       
            int CustomerID = TblCustomerManager.CreateInstant().InsertUpdate(GetTblCustomer());
            int ReservationID = TblReservationManager.CreateInstant().InsertUpdate(GetTblReservation(CustomerID));
            //TblReservationEntity _TblReservation = GetTblReservation(CustomerID);
            //TblReservationManager.CreateInstant().InsertUpdate(_TblReservation);//viet ham nay lai giong nhu customer tra ve ID
            List<TblReservationDetailEntity> ls = GetTblReservationsDetail(ReservationID);
            for (int i = 0; i < ls.Count; i++)
            {
                TblReservationDetailManager.CreateInstant().InsertUpdate(ls[i]);
            }           
         Reset();
         string messagebox = "Quý Khách đã đặt phòng thành công";
         StringBuilder stringbuilder = new StringBuilder();
         stringbuilder.Append("<script language='javascript'>");
         stringbuilder.Append("alert(\"" + messagebox + "\");");
         stringbuilder.Append("<" + "/script>");
         Response.Write(stringbuilder.ToString());
    }
    protected void btnXoa_Click(object sender, EventArgs e)
    {
         Reset();
    }
    void Reset()
    {
        txtName.Text = "";
        txtEmail.Text = "";
        txtAddress.Text = "";
        txtPhone.Text = "";
        txtNote.Text = "";
        //txtDate.Text = "";
    }
}
